package com.subaiqiao.yunshiyuan.entrance.ysy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.subaiqiao.yunshiyuan.basis.utils.Result;
import com.subaiqiao.yunshiyuan.entrance.ysy.pojo.Favorite;
import com.subaiqiao.yunshiyuan.entrance.ysy.pojo.Product;
import com.subaiqiao.yunshiyuan.entrance.ysy.pojo.ProductCategory;
import com.subaiqiao.yunshiyuan.entrance.ysy.pojo.UserInfo;
import com.subaiqiao.yunshiyuan.entrance.ysy.service.FavoriteService;
import com.subaiqiao.yunshiyuan.entrance.ysy.service.ProductCategoryService;
import com.subaiqiao.yunshiyuan.entrance.ysy.service.ProductService;
import com.subaiqiao.yunshiyuan.entrance.ysy.service.UserInfoService;
import com.subaiqiao.yunshiyuan.entrance.ysy.vo.ProductVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 曹朝禹
 * @since 2022-05-13
 */
@RestController
@RequestMapping("/ysy/product")
@Api(tags = "商品信息")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private FavoriteService favoriteService;

    @Autowired
    private ProductCategoryService productCategoryService;

    @ApiOperation("分页查询所有商品信息")
    @GetMapping("/FindProductPage.do")
    public Result FindProductPage(
            @RequestParam @ApiParam("页码") Integer pageNum,
            @RequestParam @ApiParam("数量") Integer pageSize,
            @RequestParam(defaultValue = "") @ApiParam("商品条形码") String productBar,
            @RequestParam(defaultValue = "") @ApiParam("商品名称") String productName,
            @RequestParam(defaultValue = "hot_index") @ApiParam("排序类型") String sortType,
            @RequestParam(defaultValue = "") @ApiParam("商品类型Id") String productCategoryId
    ){
        QueryWrapper<ProductVO> wrapper = new QueryWrapper<>();
        if(!productBar.equals("")){
            wrapper.eq("product_bar",productBar);
        }
        if(!productName.equals("")){
            wrapper.and(
                    query->query.like("product_name",productName)
                            .or()
                    .like("a.product_category_name",productName)
                            .or()
                    .like("product_intro",productName)
            );
        }
        if(!sortType.equals("")){
            wrapper.orderByDesc("recommendation_index",sortType);
        }
        if(!productCategoryId.equals("")){
            ProductCategory productCategory = productCategoryService.getById(productCategoryId);
            wrapper.eq("product_type",productCategoryId);
            if(productCategory.getProductCategoryParentId()!=null){
                wrapper.or().eq("product_type",productCategory.getProductCategoryParentId());
            }
        }
        Page<ProductVO> page = new Page<>(pageNum,pageSize);
        return Result.success(productService.FillProductPage(page,wrapper));
    }

    @ApiOperation("查询商品详情")
    @GetMapping("/FindProductDetails.do")
    public Result FindProductDetails(HttpServletRequest request, @RequestParam @ApiParam("商品编号") String ProductBar){
        String token = request.getHeader("Authorization");
        if(token==null||token.equals("")){
            QueryWrapper<ProductVO> wrapper = new QueryWrapper<>();
            wrapper.eq("product_bar",ProductBar);
            return Result.success(productService.FillProductDetails(wrapper));
        }
        QueryWrapper<UserInfo> wrapper_user = new QueryWrapper<>();
        wrapper_user.eq("token",token);
        UserInfo user = userInfoService.getOne(wrapper_user);
        QueryWrapper<ProductVO> wrapper = new QueryWrapper<>();
        wrapper.eq("product_bar",ProductBar);
        QueryWrapper<Product> wrapper_1 = new QueryWrapper<>();
        wrapper_1.eq("product_bar",ProductBar);
        Product product = productService.getOne(wrapper_1);
        if(product!=null && user!=null){
            product.setProductCheckQuantity(product.getProductCheckQuantity()+1);
            productService.updateById(product);
            Favorite favorite = new Favorite();
            favorite.setUsername(user.getUsername());
            favorite.setFavoriteType(0);
            favorite.setProductBar(ProductBar);
            favorite.setIsBuy(0);
            favorite.setStatus(0);
            favoriteService.save(favorite);
        }
        return Result.success(productService.FillProductDetails(wrapper));
    }

    @ApiOperation("查询商品详情(不增加浏览量)")
    @GetMapping("/GetProductDetails.do")
    public Result GetProductDetails(@RequestParam @ApiParam("商品编号") String ProductBar){
        QueryWrapper<ProductVO> wrapper = new QueryWrapper<>();
        wrapper.eq("product_bar",ProductBar);
        return Result.success(productService.FillProductDetails(wrapper));
    }

    @ApiOperation("新增商品")
    @PostMapping("/InsertProduct.do")
    public Result InsertProduct(Product product){
        if(product.getProductId()!=null){
            product.setCreateDatetime(new Date());
            return Result.success(productService.updateById(product));
        }else{
            product.setSalesVolume(0);
            product.setProductCheckQuantity(0);
            product.setUpdateDatetime(new Date());
            return Result.success(productService.save(product));
        }
    }

    @ApiOperation("删除一个商品")
    @DeleteMapping("/DeleteProduct.do")
    public Result DeleteProduct(@RequestParam @ApiParam("商品编号") Integer productId){
        return Result.success(productService.removeById(productId));
    }

    @ApiOperation("查询所有商品数量")
    @GetMapping("/FindProductCount.do")
    public Result FindProductCount(){
        return Result.success(productService.FindProductCount());
    }

    @ApiOperation("查询在售商品数量")
    @GetMapping("/FindInSellingGoodsCount.do")
    public Result FindInSellingGoodsCount(){
        return Result.success(productService.FindInSellingGoodsCount());
    }

    @ApiOperation("查询缺货商品数量")
    @GetMapping("/FindShortageOfGoodsCount.do")
    public Result FindShortageOfGoodsCount(){
        return Result.success(productService.FindShortageOfGoodsCount());
    }

    @ApiOperation("查询昨日上新商品数量")
    @GetMapping("/FindProductCountYesterdayRegister.do")
    public Result FindProductCountYesterdayRegister(){
        return Result.success(productService.FindProductCountYesterdayRegister());
    }

//    @ApiOperation("测试")
//    @GetMapping("/test.do")
//    public Result test(){
//        return Result.success(productService.test());
//    }

}
